package com.jiang.jixiang.ui;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jiang.jixiang.R;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.bean.BillingRecordsBean;
import com.jiang.jixiang.databinding.ActivityBillingBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.jiang.jixiang.ui.adapter.BillingAdapter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.ws.universal.tools.base.BaseActivity;
import com.ws.universal.tools.base.ManageActivity;
import com.ws.universal.tools.utils.GsonUtils;
import com.ws.universal.tools.utils.StringUtils;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 账单记录
 */
public class BillingActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener, NetWorkListener {
    public int pageNumber = 1;
    public int pages;
    private ActivityBillingBinding binding;
    private BillingAdapter billingAdapter;
    private BillingRecordsBean billingRecordsBean;

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        ManageActivity.putActivity(this.getClass().getSimpleName(), this);
        binding = ActivityBillingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }

    @Override
    protected void initView() {
        binding.inTitle.titleTextTv.setText(R.string.title_billing);
        binding.inTitle.titleLeftBtn.setOnClickListener(view -> {
            finish();
        });
        binding.refreshLayoutlive.setOnRefreshListener(this);//加载更多
        binding.refreshLayoutlive.setOnLoadMoreListener(this);//加载更多
        binding.refreshLayoutlive.autoRefresh();
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rcyData.setLayoutManager(manager);

        billingAdapter = new BillingAdapter();
        binding.rcyData.setAdapter(billingAdapter);


    }

    private void loadData() {
        Map<String, String> params = new HashMap<>();
        params.put("page_no", StringUtils.getString(pageNumber));
        params.put("page_size", StringUtils.getString(page_size));
        okHttpModel.post(ApiUrl.paymentLog_Api, params, ApiUrl.paymentLog_Api_ID, this);
    }

    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        JSONObject data = jsonData.optJSONObject("data");
        switch (id) {
            case ApiUrl.paymentLog_Api_ID:
                billingRecordsBean = GsonUtils.parseJObject(data.toString(), BillingRecordsBean.class);
                if (billingRecordsBean != null) {
                    pages = billingRecordsBean.getTotal() % page_size;
                    if (pages == 0) {
                        pages = billingRecordsBean.getTotal() / page_size;
                    } else {
                        pages = billingRecordsBean.getTotal() / page_size + 1;
                    }
                    if (pageNumber > 1) {
                        for (int i = 0; i < billingRecordsBean.getList().size(); i++) {
                            billingAdapter.getData().add(billingRecordsBean.getList().get(i));
                        }
                        billingAdapter.notifyDataSetChanged();
                    } else {
                        billingAdapter.setList(billingRecordsBean.getList());
                    }
                    if (billingAdapter.getData().size() == 0) {
                        binding.viewStub.inflate();
                    }
                }

                break;
        }
    }

    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        ToastUtils.showShort(commonality.getMessage());
    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.getMessage());
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            pageNumber = 1;
            loadData();
            refreshLayout.finishRefresh();
            refreshLayout.resetNoMoreData();//setNoMoreData(false);//重置初始状态
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            if (pageNumber >= pages) {
                refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
            } else {
                pageNumber++;
                loadData();
                refreshLayout.finishLoadMore();
            }
        }
    }

    @Override
    protected void FinishDesTroy() {
        ManageActivity.removeActivity(this.getClass().getSimpleName());
    }

}
